package com.mywang.weitravel.modules.image;

import android.content.Context;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.engine.ImageEngine;
import com.mywang.weitravel.R;

public class GlideImageEngine implements ImageEngine {

    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    @Override
    public void loadImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) {
        Glide.with(context).load(url).into(imageView);
    }

    /**
     * 加载相册目录
     *
     * @param context   上下文
     * @param url       图片路径
     * @param imageView 承载图片ImageView
     */
    @Override
    public void loadFolderImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) {
        RequestOptions options = RequestOptions.bitmapTransform(new RoundedCornersTransformation(8, 0, RoundedCornersTransformation.CornerType.ALL))
                .placeholder(R.drawable.picture_icon_placeholder)
                .fallback(R.drawable.picture_icon_placeholder)
                .error(R.drawable.picture_icon_placeholder)
                .override(100, 100)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(options)
                .into(imageView);
    }

    public void loadImage(@NonNull String url, @NonNull ImageView imageView, int radius) {
        RequestOptions options = RequestOptions.bitmapTransform(new RoundedCornersTransformation(radius, 0, RoundedCornersTransformation.CornerType.ALL))
                .placeholder(R.drawable.picture_icon_placeholder)
                .fallback(R.drawable.picture_icon_placeholder)
                .error(R.drawable.picture_icon_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(imageView.getContext())
                .asBitmap()
                .load(url)
                .apply(options)
                .into(imageView);
    }

    public void loadImage(@NonNull String url, @NonNull ImageView imageView, int defaultResourceId, int radius) {
        RequestOptions options = RequestOptions.bitmapTransform(new RoundedCornersTransformation(radius, 0, RoundedCornersTransformation.CornerType.ALL))
                .placeholder(defaultResourceId)
                .fallback(defaultResourceId)
                .error(defaultResourceId)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(imageView.getContext())
                .asBitmap()
                .load(url)
                .apply(options)
                .into(imageView);
    }

    public void loadImage(@NonNull String url, @NonNull ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.picture_icon_placeholder)
                .fallback(R.drawable.picture_icon_placeholder)
                .error(R.drawable.picture_icon_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(imageView.getContext())
                .asBitmap()
                .load(url)
                .apply(options)
                .into(imageView);
    }


    /**
     * 加载gif
     *
     * @param context   上下文
     * @param url       图片路径
     * @param imageView 承载图片ImageView
     */
    @Override
    public void loadAsGifImage(@NonNull Context context, @NonNull String url,
                               @NonNull ImageView imageView) {
        Glide.with(context)
                .asGif()
                .load(url)
                .into(imageView);
    }

    /**
     * 加载图片列表图片
     *
     * @param context   上下文
     * @param url       图片路径
     * @param imageView 承载图片ImageView
     */
    @Override
    public void loadGridImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.picture_image_placeholder)
                .fallback(R.drawable.picture_image_placeholder)
                .error(R.drawable.picture_image_placeholder)
                .override(200, 200)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }


    private GlideImageEngine() {
    }

    private static GlideImageEngine instance;

    public static GlideImageEngine getInstance() {
        if (null == instance) {
            synchronized (GlideImageEngine.class) {
                if (null == instance) {
                    instance = new GlideImageEngine();
                }
            }
        }
        return instance;
    }
}